package com.apes.scm.masterdata.finance.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Creator: zouyc
 * CreateDate: 2020-06-10 9:12
 * Description:
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name= CrossDefiIC.CONST_TABLE_NAME)
public class CrossDefiIC extends Model {
    public static final String CONST_TABLE_NAME = "mas_cross_dic";

    @Id
    @Column(name="id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "异业合作类型", padding = 4)
    @Comment("异业合作类型编码")
    private String id;

    @Column(name = "name", nullable = false)
    @Comment("异业合作类型名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("客户")
    private PartyRole partner;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("合作公司")
    private PartyRole company;  //

    @Comment("有效标记")
    @Column(name = "valid",columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    private boolean valid = true;

    @OneToMany(mappedBy = "crossDefiIC", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CrossICServe> crossICServes = new ArrayList<CrossICServe>(); //从表
    public List<CrossICServe> getCrossICServes(){
        for(CrossICServe crossICServe : crossICServes){
            crossICServe.setCrossDefiIC(this);
        }
        return crossICServes;
    }

    @OneToMany(mappedBy = "crossDefiIC", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CrossDept> crossDepts = new ArrayList<CrossDept>(); //从表
    public List<CrossDept> getCrossDepts(){
        for(CrossDept crossDept : crossDepts){
            crossDept.setCrossDefiIC(this);
        }
        return crossDepts;
    }
}
